<?php
session_start();
require_once ("../src/db/connectdatabase.php");
require_once ("../model/PartNumberToMoney.php");

function loaddataCardDetail($cardtype, $cardlevel, $tienmat, $khuvuc, $sqltem) {
	$con = new ConnectDB();
	
	$sql = "SELECT carddetailid, bankid, bankname, banklogo, cardtypename, cardtypelogo, balancetransfer, monthstransfer, interestrate, annualfee, interestfreedays FROM carddetail WHERE cardtypeid LIKE '%".$cardtype."%' AND idcardlevel LIKE '%".$cardlevel."%' AND phiruttien LIKE '%".$tienmat."%' AND areaofcards LIKE '%".$khuvuc."%' AND isuse=1".$sqltem;
	return $con -> getvalueString($sql);
}

if(isset($_POST['act'])) {
	if($_POST['act'] == "load") {
		if(isset($_POST['hmthe']) && isset($_POST['lt']) && isset($_POST['lv']) && isset($_POST['tm']) && isset($_POST['kv'])) {
			$hanmucthe = $_POST['hmthe']; 
			$cardtype = $_POST['lt'];
			$cardlevel = $_POST['lv'];
			$tienmat = $_POST['tm'];
			$khuvuc = $_POST['kv'];
			$sqltemp = "";
			if($hanmucthe != "") {
				$sqltemp = " AND cardlimit <= ".$hanmucthe;
			}
			
			$arrresult = loaddataCardDetail($cardtype, $cardlevel, $tienmat, $khuvuc, $sqltemp);
			if(count($arrresult) > 0) {
				for($i = 0; $i < count($arrresult); $i++) {
					$arrresult[$i][6] = changnumbermoney($arrresult[$i][6]);
					$arrresult[$i][9] = changnumbermoney($arrresult[$i][9]);
				}
				echo json_encode($arrresult);
			} else {
				echo json_encode("null");
			}
			
			if(isset($_SESSION['arrcomparecarddetail'])) {
				unset($_SESSION['arrcomparecarddetail']);
			}
		}
	} else if($_POST['act'] == "sscom") {
		if(isset($_POST['pr']) && isset($_POST['idre'])) {
			$arrcompare = array();
			if(isset($_SESSION['arrcomparecarddetail'])) {
				$arrcompare = $_SESSION['arrcomparecarddetail'];
			}
			$acc = $_POST['pr'];
			$idcom = $_POST['idre'];
			if($acc == "add") {
				$arrcompare.array_push($arrcompare, $idcom);
			} else if($acc == "del") {
				$arrcompare = array_diff($arrcompare, array($idcom));
			}
			$_SESSION['arrcomparecarddetail'] = $arrcompare;
			echo json_encode("ok");
		}
	}
} else {
	echo json_encode("null");
}
?>